package cn.tedu.ivos.geofence.service.impl;


import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;


    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询列表业务，参数：{}", geofenceQuery);
        List<GeofenceVO> list=geofenceMapper.selectGeofence(geofenceQuery);
//        将该围栏下绑定车辆计数
        for (int i=0;i<list.size();i++){
            geofenceQuery.setGeofenceId(list.get(i).getId()); //获取围栏id
            geofenceQuery.setGeofenceBindStatus("1");
            Integer totalNum=vehicleMapper.countVehicle(geofenceQuery); //根据围栏id查询绑定在该围栏下的车辆总数
            list.get(i).setTotalNum(totalNum);
            geofenceQuery.setGeofenceBindStatus("0");
            geofenceQuery.setGeofenceId(null);
            Integer availableNum=vehicleMapper.countVehicle(geofenceQuery);
            list.get(i).setAvailableNum(availableNum);
            log.debug("查询列表业务，结果：{}", list.get(i));

        }

        return list;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("保存围栏数据业务,参数：{}", geofenceSaveParam);
        Geofence geofence=new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        if (geofence.getId()!=null){

            geofence.setUpdateTime(new Date());
            geofenceMapper.updatefence(geofence);

        }else {
            geofence.setCreateTime(new Date());
            geofence.setStatus("1");//新增时每个围栏都是启用状态
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏业务，参数：{}",id);
//        需要判断给围栏中是否有绑定车辆，如有就不能删除
//        1.封装车辆查询的DTO层对象VehicleQuery
        VehicleQuery vehicleQuery=new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
//        调用车辆数据层查询是否有绑定在当前围栏上的车辆
        List<VehicleVO> list=vehicleMapper.selectVehicle(vehicleQuery);
        if (list.size()>0 && list !=null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
//            如果该围栏没有绑定车辆，就可以正常删除
            geofenceMapper.deleteGeofence(id);
        }
    }
}
